package activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.activity.viewModels
import com.blankj.utilcode.util.ImageUtils
import com.blankj.utilcode.util.UriUtils
import com.wd.common.base.BaseActivity
import com.wd.common.viewmodel.MyViewModel
import com.wd.rim.R
import com.wd.rim.databinding.ActivityTalkBinding
import com.wd.rim.databinding.ActivityTiwenBinding
import java.io.File

class TiwenActivity : BaseActivity<ActivityTiwenBinding,Any?>() {
    var file: File? =null
    val list= mutableListOf<File>()
    private val mviewmodel: MyViewModel by viewModels()
    override fun getViewBinding()=ActivityTiwenBinding.inflate(layoutInflater)

    override fun initData() {
       binding.imageId.setOnClickListener {
           if(file==null){
               startActivityForResult(Intent(Intent.ACTION_PICK).setType("image/*"),1)
           }else{

           }
       }

        binding.send.setOnClickListener {
            val title = binding.etTitle.text.toString()
            mviewmodel.getTiwen(
                title,
                "北京",
                list
            )
            finish()
        }
        mviewmodel.tiwen.observe(this){
            Toast.makeText(this,it.message,Toast.LENGTH_SHORT).show()
        }


        

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==1&&data!=null){
            val data1 = data.data
            file= UriUtils.uri2File(data1)
            list.add(file!!)
            val bitmap = ImageUtils.getBitmap(file)
            binding.imageId.setImageBitmap(bitmap)
        }
    }
}